function saveNote() {
	// test here
	if (!isOnline) {
		alert("您已经离线，无法保存");
		return;
	}

	var v_editor = UM.getEditor('editor').getContent();
	var v_name = $('#name').val();
	var v_tags = $('#tagt').val();
	var v_id = $("#id").val();
	var v_alias = $("#alias").val();
	var v_author_ = $("#author_").val();
	var v_state = $("input[name='state']:checked").val();
	if (v_name == null || v_name == "") {
		$("#msg").html("“名称”必填");
		return;
	}

	$.post("/webnote/save", {
		noteContent : v_editor,
		name : v_name,
		tags : v_tags,
		id : v_id,
		alias : v_alias,
		author_ : v_author_,
		state : v_state
	}, function(id, textStatus) {
		if (id > 0) {
			$("#id").val(id);
			$("#msg").html("保存成功 - " + new Date());
			initSave($('#save'));
		} else {
			$("#msg").html("保存失败 - " + new Date());
		}
	});
};

function delnote() {

	var v_name = $('#name').val();
	var v_id = $("#id").val();
	var dialog_s = $('#dialog_simple');

	if (v_id == null || v_id == "") {
		dialog_s.dialog({
			width : 300,
			buttons : {
				"知道了" : function() {
					$(this).dialog("close");
				}
			}
		});
		dialog_s.html("请先从左侧选择笔记");
		dialog_s.dialog('open');
		return false;
	}

	dialog_s.dialog({
		width : 300,
		buttons : {
			"确认" : function() {
				window.location.href = "/webnote/delete?id=" + v_id;
			},
			"取消" : function() {
				$(this).dialog("close");
			}
		}
	});
	dialog_s.html("删除 " + v_name + " ？");
	dialog_s.dialog('open');
	return false;
};

function recoverSave(save) {
	save.removeAttr("disabled");
	save.removeClass("ui-state-disabled");
}

function initSave(save) {
	save.attr("disabled", "disabled");
	save.addClass("ui-state-disabled");
}

$(function() {
	var save = $('#save');
	var del = $('#delete');
	var add = $('#add');
	var exp = $('#exp');

	var noteContent = $('#noteContent');
	var eduitoolbar = $('.edui-toolbar');
	var name = $('#name');
	var tags = $('#tagt');

	// set up style
	save.button();
	del.button();
	$('#dialog_simple').dialog({
		autoOpen : false
	});
	$('#radioset').buttonset();

	// can save change
	if (noteContent) {
		noteContent.on('keypress', function() {
			recoverSave(save);
		});
		noteContent.on('keydown', function() {
			recoverSave(save);
		});
	}
	if (eduitoolbar) {
		eduitoolbar.on('mouseup', function() {
			recoverSave(save);
		});
		eduitoolbar.on('mousedown', function() {
			recoverSave(save);
		});
	}
	if (name) {
		name.on('keypress', function() {
			recoverSave(save);
		});
		name.on('keydown', function() {
			recoverSave(save);
		});
	}
	if (tags) {
		tags.on('keypress', function() {
			recoverSave(save);
		});
		tags.on('keydown', function() {
			recoverSave(save);
		});
	}

	// action: save is in html

	if (del) {
		del.on('click', delnote);
	}
	;

	$(window).bind('beforeunload', function() {
		if ($('#save').attr("disabled") == "disabled") {
			return;
		} else {
			return "您输入的内容尚未保存，确定要离开此页吗？";
		}
	});

	$(document).on('keydown', function(evt) {
		if ((evt.keyCode == 83) && (evt.ctrlKey)) {
			saveNote();
			return false;
		}
	});

	// action: add
	if (add) {
		add.on('click', function() {
			window.location.href = "/webnote";
		});
	}
	;

	// action: export
	if (exp) {
		exp.on('click', function() {
			window.location.target = "_blank";
			window.location.href = "/webnote/export";
		});
	}
	;

});